
var currentPage;
var USELOG = false;
var g_cur_row_seq_no =0;
var lastMarker;

//////////////////////////////////////////////////////////////////////////////////////
function gotoPage(pageName) {
	var pagestr = pageName + "?category=" + g_category + "&sec=" + g_section;
	window.location= pagestr;	
}


////////////////////////////////////////////////////////////////////////////////////
function init() {

	var args = getArgs();
	if (args.category) {
		g_category = args.category;
		g_section = args.sec;
	}
	
	initMap();
	
	GEvent.addListener(map, "click", function(overlay, latlng) {
		if(!overlay) {
			var lng = latlng.lng();
			var lat = latlng.lat();
	
			document.getElementById("txt_lat").value = lat;
			document.getElementById("txt_lng").value = lng;
			
			var latlng = new GLatLng(parseFloat(lat),parseFloat(lng));
			lastMarker = createMarker(latlng, content);
			map.addOverlay(lastMarker);			
		}
	}
);
	
	
	handleResize(true);
}


/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
function storeMarker(){

	var lng = document.getElementById("txt_lng").value;
	var lat = document.getElementById("txt_lat").value;
	var theURL = document.getElementById("txt_url").value;
	var theLabel = document.getElementById("txt_label").value;
	var nickName = document.getElementById("txt_nickname").value;

	if (lng == "") {
		alert("You must click the map to indicate the location of your marker.");
		return;
	}

	if (theLabel.length < 3) {
		alert("You must provide a label for the marker you are adding, at least 3 characters in length. This will display on the map to identify the marker.");
		return;
	}
	
	if (nickName.length < 3) {
		alert("You must provide some name to identify you as the creator of this marker.");
		return;
	}	
	
	if ( ! validURL(theURL) ) {
		alert("You must provide a valid URL to add a marker. A valid URL looks like:  HTTP://www.mypage.com");
		return;
	}
	
	if (g_section == "") {
		alert("Somehow we lost the section ID. Sorry, but you'll have to return to your course and start over.");
		return;
	}
	
	if (g_category == "") {
		alert("Somehow we lost the module ID. Sorry, but you'll have to return to your course and start over.");
		return;
	}

	if (theLabel.indexOf("'") >= 0) {
		alert("The apostrophe character is not allowed in a place label. Please remove the character and try again.");
		return;
	}

	var getVars =  "?loc_label=" + theLabel
	+ "&loc_url=" + theURL
	+ "&lng=" + lng
	+ "&lat=" + lat 
	+ "&loc_nickname=" + nickName
	+ "&loc_category=" + g_category
	+ "&loc_section=" + g_section
	+ "&cid=" + g_courseid;
	//GLog.write(getVars);

	var request = GXmlHttp.create();
	request.open('GET', '../shared_v2/mapblog_data_update.php' + getVars, true);
	//GLog.write()	
	request.onreadystatechange = function() {
		//Glog.write(request.readyState);		
		if (request.readyState == 4) {
			var xmlDoc = request.responseXML;
			//alert(request.responseXML);
			var responseNode = xmlDoc.documentElement;
			var type = responseNode.getAttribute("type");
			var content = responseNode.firstChild.nodeValue;
			//check to see if it was an error or success
			if(type!='success') {
				alert("We are unable to save the maker because of the error: " + content);
				GLog.write(content);
			} else {
				
				theLen= (content.length-9) - 8;
				g_cur_row_seq_no = content.substr(8, theLen);
				//Create a new marker and add it's info window.
				var latlng = new GLatLng(parseFloat(lat),parseFloat(lng));
				lastMarker = createMarker(latlng, content);
				map.addOverlay(lastMarker);
				map.closeInfoWindow();
				alert("Your Placemark was Saved");
				//var yourPageURL = "The URL of the page you linked with this marker is: \n\n" + theURL;
				//alert(yourPageURL);
				
				//var warningMsg = "Your point was added. \n\n When you close this dialog box the page your maker was linked to will be displayed. "
				//+ "If the page does not display correctly, you can use the 'Delete the Last Point I added' link (above, right) to remove the marker and try again. \n\n"
				//+ "Otherwise, close the window and return the to the main MapBlog page to test your new marker.";
				//alert(warningMsg);
				
				//w = window.open(theURL,"","width=800,height=350,status=yes,resizable=yes, scrollbars=yes");

			}
			
		}
	}
	request.send(null);
	return false;
}

/////////////////////////////////////////////////////////////////////////////////////
function deletePoint() {
	if (g_cur_row_seq_no == 0) {
		alert("You can only delete a marker added during this visit to the Add Locations page. We were not able to find that marker so we can't remove it. For assistance deleting other markers contact your instructor.");
		return;
	}
	
	GDownloadUrl('../shared_sp08/mapblog_data_delete.php?row=' + g_cur_row_seq_no, function(data, responseCode) {
		g_cur_row_seq_no = 0;

		if (data == 1) {
			lastMarker.remove();
			alert("The last marker you added has been removed. You can add another marker, or return to the MapBlog");
		} else {
			alert("Some problem has occured. Please try again. ");
		}
	});	
}

/////////////////////////////////////////////////////////////////////////////////////
function createMarker(latlng, html) {
	var marker = new GMarker(latlng);
	GEvent.addListener(marker, 'click', function() {
		var markerHTML = html;
		marker.openInfoWindowHtml(markerHTML);
	});
	return marker;
}

/////////////////////////////////////////////////////////////////////////////////////
function retrieveMarkers() {
	var request = GXmlHttp.create();

	//tell the request where to retrieve data from.
	request.open('GET', 'retrieveMarkers.php', true);

	//tell the request what to do when the state changes.
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			var xmlDoc = request.responseXML;

			var markers = xmlDoc.documentElement.getElementsByTagName("marker");
			for (var i = 0; i < markers.length; i++) {
				var lng = markers[i].getAttribute("lng");
				var lat = markers[i].getAttribute("lat");
				//check for lng and lat so MSIE does not error
				//on parseFloat of a null value
				if(lng && lat) {
					var latlng = new GLatLng(parseFloat(lat),parseFloat(lng));

					var html = '<div><b>Name (Label)</b> '
					+ markers[i].getAttribute("loc_label")
					+ '</div><div><b>URL</b> '
					+ markers[i].getAttribute("loc_url")
					+ '</div>';

					var marker = createMarker(latlng, html);
					map.addOverlay(marker);
					
					
				}
			} //for
		} //if
	} //function

	request.send(null);
}


////////////////////////////////////////////////////////////////////////////////////
function checkURL() {
	 window.open (document.getElementById("txt_url").value,"Your page should appear in the window...","status=1,toolbar=1");
}

/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
// This should be removed from production code ... but it is very useful
/////////////////////////////////////////////////////////////////////////////////////
function writeLog(message) {
	if (USELOG) {
		GLog.write(message);	
	};
}


window.onresize = handleResize;
//window.onload = init;
